package pl.edu.agh.jemo.gui.panels.crossover;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.operator.crossover.impl.LinearCrossover;

/**
 * Configuration panel allowing to adjust linear crossover parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing crossover instance, using reference passed in constructor.
 * It also extends classic crossover panel, so it contains all components used by classic crossover.
 * 
 * @author Marcin Zbijowski
 *
 */
public class LinearCrossoverConfigPanel extends ClassicCrossoverConfigPanel {

	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = -3245515709867989925L;
	/**
	 * Reference to linear crossover instance configured by panel.
	 */
	private LinearCrossover crossover;
	/**
	 * Field allowing to modify chance of crossover on single dimension.
	 */
	protected JTextField dimCrossChanceEdit;
	
	/**
	 * Creates Panel instance associated with given crossover reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param crossoverReference reference to linear crossover, that needs configuration. 
	 */
	public LinearCrossoverConfigPanel(LinearCrossover crossoverReference) {
		super(crossoverReference);
		crossover = crossoverReference;
		
		JLabel dimLabel = new JLabel("Single dimension crossover channce:");
		dimLabel.setBounds(10, 145, 150, 15);
		dimCrossChanceEdit = new JTextField(String.valueOf(crossover.getDimensionsToCross()));
		dimCrossChanceEdit.setBounds(10, 165, 150, 20);
		dimCrossChanceEdit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					crossover.setDimensionsToCross(Double.parseDouble(dimCrossChanceEdit.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
		
		add(dimLabel);
		add(dimCrossChanceEdit);
	}

}
